/**
 * @author: Loy-winters1224@163.com
 * @date: 2014-8-21-下午6:20:11
 */
package net.loyintean.pearlshell.wechat.distribute.impl;

import org.apache.log4j.Logger;

import net.loyintean.pearlshell.wechat.common.MessageTypeEnum;
import net.loyintean.pearlshell.wechat.distribute.IMessageDistributor;
import net.loyintean.pearlshell.wechat.model.BaseMessageModel;
import net.loyintean.pearlshell.wechat.model.common.TextMessageModel;
import net.loyintean.pearlshell.wechat.service.IMessageProcesser;
import net.loyintean.pearlshell.wechat.service.encrypt.EncrIntroduceMessageProcesserImpl;
import net.loyintean.pearlshell.wechat.service.encrypt.EncryptMessageProcesser;

/**
 * 
 * @author Loy-winters1224@163.com
 * @version 2014-8-21.1.0.0
 */
public class EncrMessageDistributor implements IMessageDistributor {

	private static final Logger LOGGER = Logger.getRootLogger();

	private static final String introduceKey = "加密", ecnryptKey = "加密@";

	private IMessageProcesser introducer = new EncrIntroduceMessageProcesserImpl(),
			encrypter = new EncryptMessageProcesser();

	@Override
	public IMessageProcesser distribute(BaseMessageModel message) {

		assert message != null;

		LOGGER.info("EncrMessageDistributor#distribute(" + message + ")");

		TextMessageModel textMessage;

		if (MessageTypeEnum.text.equals(message.getMsgType())) {
			textMessage = (TextMessageModel) message;

			String content = textMessage.getContent();

			LOGGER.info("EncrMessageDistributor#distribute(" + message
					+ "):contents=" + content);

			if (introduceKey.equals(content) || ecnryptKey.equals(content)) {
				return introducer;
			} else if (content.startsWith(ecnryptKey)) {
				return encrypter;
			}

		}

		return null;
	}
}
